<?php

	
	require_once 'ConnexionService.php';
	require_once 'ConnectDb.php';
	require_once 'AppMessages.php';
	
	class ConfirmationService{
		
		public function confirm( $key ) {
				
			$connectDb = ConnectDb::getInstance()->getConnexion(); //instance unique a la bdd
			//$key = $connectDb->quote($key);
			$requete = 'SELECT name,password,email FROM players_notconfirm WHERE confirmKey = :key';
			$prep = $connectDb->prepare( $requete );
			$prep->bindValue( ":key", $key, PDO::PARAM_STR );
			$prep->execute();
			
			$data = $prep->fetch();
			
			if ( $data )
			{
				$confirm = new ConnexionService();
				
				/*ajout a la bdd players*/
				$confirm->confirmInscription( $data["name"], $data["password"], $data["email"] );
								
				/*supprimer la ligne de la table notconfirm*/
				$requete = 'DELETE FROM players_notconfirm WHERE confirmKey = :key';
				$prep = $connectDb->prepare( $requete );
				$prep->bindValue( ":key", $key, PDO::PARAM_STR );
				$prep->execute();
				
				return AppMessages::INSCRIPTION_CONFIRMED;
			}
			else 
			{
				return AppMessages::INVALID_USER;
			}
		}
		
	}
?>